package br.edu.ufcg.dsc.actions.resource;

import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletDispatcherResult;

import br.edu.ufcg.dsc.actions.BaseLoggedAction;
import br.edu.ufcg.dsc.domain.Resource;
import br.edu.ufcg.dsc.domain.resource.Article;
import br.edu.ufcg.dsc.services.ResourceService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@Result(name = "success", type = ServletDispatcherResult.class, value = "/jsp/resource/article.jsp")
public class FindArticleAction extends BaseLoggedAction implements Preparable, ModelDriven<Article>{

	private int resourceId;
	private Article article;
	private static final long serialVersionUID = 1L;

	public void prepare() throws Exception {
		Resource resource = ResourceService.find(resourceId);
		if (resource != null)
			article = new Article(resource.getMarc());
		else article = new Article();
	}

	public int getResourceId() {
		return resourceId;
	}

	public void setResourceId(int resourceId) {
		this.resourceId = resourceId;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Article getModel() {
		return article;
	}

	@Override
	protected String executeIfLogged() throws Exception {		
		return ActionSupport.SUCCESS;
	}

}
